package service;

import java.io.IOException;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import ejb.PositionDescriptionEJB;
import ejb.PositionDescriptionEJBRemote;
import entityJPA.Candidate;
import entityJPA.Department;
import entityJPA.Positiondescription;

/**
 * Session Bean implementation class PositionService
 */
@Stateless
@LocalBean
public class PositionDescriptionService {
	@EJB PositionDescriptionEJBRemote positiondescEJB;
	
	private static Logger logger = Logger.getLogger(UserService.class.getName());


	/*
     * Default constructor. 
     */
    public PositionDescriptionService() {
        // TODO Auto-generated constructor stub
		FileHandler fh;
		try {
			fh = new FileHandler("C:/PositionLoggerFile.log", true);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			logger.addHandler(fh);
			// logger.log(Level.SEVERE, "Error doing XYZ");
			// logger.info("My first log");
		} catch (SecurityException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void insert(Positiondescription p){
    	try{
    		positiondescEJB.persistPositiondescription(p);
    		logger.info("inserted position description:"+p.toString());
    	}catch(Exception e){
    		logger.log(Level.SEVERE,e.getMessage());
    		throw e;
    	}
    }
    
    public void edit(Positiondescription pos) throws Exception{
    	try{
    		positiondescEJB.mergePositiondescription(pos); 
    		logger.info("edited position description:"+pos.toString());
    	}catch(Exception e){
    		logger.log(Level.SEVERE,e.getMessage());
    		throw e;
    	}
    }
    
    public Positiondescription findPositiondescriptionById(int id){
    	List<Positiondescription> lista=positiondescEJB.getPositiondescriptionFindAll();
    	for(int i=0;i<lista.size();i++){
    		if(lista.get(i).getIdPositionDescription()==id){
    			return lista.get(i);
    		}
    	}return null;   		
    }
    public int findPositiondescriptionByValues(Positiondescription pd){
    	List<Positiondescription> lista=positiondescEJB.getPositiondescriptionFindAll();
    	for(int i=0;i<lista.size();i++){
    		if(lista.get(i).equals(pd)){
    			return lista.get(i).getIdPositionDescription();
    		}
    	}return 0;   		
    }
}
